<%
var total = 0;
var total_weight = 0;
var order_specs = [];
for(var i=0; i<info.length; i++){
  total += Number(info[i].product.msrp)*Number(info[i].amount);
  total_weight += Number(info[i].product.weight);
  var order_spec = {
    id: info[i].product.id,
    model: info[i].product.model,
    gst: info[i].product.gst,
    require_gst: 0,
    gst_amount: 0,
    brief_name: info[i].product.brief_name,
    sn: info[i].product.sn,
    weight: info[i].product.weight,
    price: info[i].product.msrp,
    quantity: info[i].amount,
    warehouse:{},
    amount: Number(info[i].amount)*Number(info[i].product.msrp)
  };
  order_specs.push(order_spec);
};
var total_qty =0;
var products = [];
for(var i=0; i<info.length; i++){
  total_qty += Number(info[i].amount);
  products.push(info[i].product.sn);
};

%>
<style>
  .country-container {
    margin-top: 0.5em;
    margin-bottom: 0.5em;

  }
  .country-container>button {
    width: 7em;
    margin-left: 4em;
  }
  .btn-set-receive {
    float: right;
    line-height: 0.42;
    font-size: 10px;
  }
  .addr-selected {
    border: 2px #cb2527 solid;
    border-radius: 10px;
  }
</style>
<header class="mui-bar mui-bar-nav header">
  <h1 class="mui-title">提交订单</h1>
</header>
<div class="mui-content">
  <!--<h5 class="order-tit">收货人信息</h5>-->
  <!--<div class="country-container">-->
    <!--<p style="text-align: center">收货地址</p>-->
    <!--<button type="button" class="mui-btn" id="btn-cn">中国</button>-->
    <!--<button type="button" class="mui-btn" id="btn-au">澳大利亚</button>-->
  <!--</div>-->
  <!--<div class="addr-container">-->
  <!--</div>-->
  <!--<div class="add-address">-->
    <!--<a href="/user/address/add/">+新建收货地址</a>-->
    <!--<i class="am-icon-angle-right"></i>-->
  <!--</div>-->
  <h5 class="order-tit">收货人信息</h5>
  <div class="order-name">
    <p id="receiver-contact">
      收件人：<%= contact.name%>    电话：<%= contact.tel%>    地址：<%= contact.addr%>
    </p>
    <i class="am-icon-angle-right"></i>
  </div>
  <div class="h10"></div>
  <ul class="shopcart-list" style="padding-bottom: 0;">
    <% for(var i=0; i<info.length; i++){%>
    <li>
      <img src="http://auewe.oss-ap-southeast-2.aliyuncs.com/products/<%= info[i].product.sn%>_thumb.jpeg" class="shop-pic">
      <div class="order-mid">
        <div class="tit"><%= info[i].product.name%> <%= info[i].product.pinyin%></div>
        <div class="tit"> <%= info[i].product.model%></div>
        <div class="order-price">$<%= info[i].product.msrp%> <i>X<%= info[i].amount%></i></div>
      </div>
    </li>
    <% }%>
  </ul>
  <ul class="order-infor">
    <li class="order-infor-first">
      <span>商品总计：</span>
      <i>$<%= total%></i>
    </li>
    <li class="order-infor-first">
      <span>运费估算：</span>
      <i id="shippint_fee"></i>
    </li>
  </ul>
  <div class="h10"></div>
  <textarea placeholder="备注说明" class="bz-infor"></textarea>
  <div class="h10"></div>
  <div class="jl"></div>
  <div class="shop-fix">
    <div class="order-text">
      预估总额：<span></span>（实际运费以客服下单后为准）
    </div>
    <div href="tureorder.html" class="js-btn">提交订单</div>
  </div>
</div>
<script>
  $(function () {
    var order = {
      "customer": sessionStorage.getItem('customer_id'),
      "contact_sender": sessionStorage.getItem('customer_contact'),
      "contact_receiver": sessionStorage.getItem('contact_id'),
      "sn":"",
      "status":-99,
      "type":0,
      "total_quantity": Number('<%= total_qty%>'),
      "total_amount":0,
      "total_gst":0,
      "total_weight": Number('<%= total_weight%>'),
      "total_actual_weight":0,
      "total_cost":0,
      "total_cost_gst":0,
      "submitter_name":null,
      "total_inner_amount": Number('<%= total%>')
    };
    var spec = '<%= JSON.stringify(order_specs)%>';
    var order_specs = spec.replace(/&quot;/g, "'");
    var orderId = '<%= orderId%>';
    $.get('/api/order/wmsid/?order_id='+orderId, function (callback) {
      console.log(callback);
      if (callback[0].wmsid == null) {
        $.post('/users/order/', {order: JSON.stringify(order), order_specs: order_specs, orderId: orderId}, function (call, stat) {
          if (stat == 'success') {
            var total_amount = call.order.total_amount;
            $('.mui-content').append('<input type="hidden" name="order_id" value="'+call.order.id+'">');
            var shipping_fee = (call.order.total_amount - Number('<%= total%>')).toFixed(2);
            $('#shippint_fee').text(shipping_fee);
            $('.order-text>span').text('$'+call.order.total_amount);
            //window.location.href = '/users/account/orders?openid='+sessionStorage.getItem('openid');
          }
        })
      }
    })

  })
</script>
<script>
  $('.js-btn').click(function () {
    $.get('/users/order/payment?order_id=<%= orderId%>', function (call) {
      if (call == 'success') {
          mui.toast('成功提交订单');
          window.location.href='/users/account/orders/?openid='+sessionStorage.getItem('openid');
      }
    })
  })
</script>

